#import "UIVCParameters.h"
#import "UIVCLive.h"

@implementation UIVCParameters

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    /* selfview */
    self.title = @"参数设置";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg001.png"]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(gotoLive)];
    
    /* subviews */
    _labTargetIP = [[UILabel alloc] init];
    _labTargetIP.translatesAutoresizingMaskIntoConstraints = NO;
    _labTargetIP.text = @"目标:";
    _labTargetIP.textAlignment = NSTextAlignmentRight;
    _labTargetIP.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_labTargetIP];
    
    _txtTargetIP = [[UITextField alloc] init];
    _txtTargetIP.translatesAutoresizingMaskIntoConstraints = NO;
    _txtTargetIP.borderStyle = UITextBorderStyleRoundedRect;
    _txtTargetIP.placeholder = @"输入地址";
    _txtTargetIP.text = @"192.168.1.100";
    _txtTargetIP.autocorrectionType = UITextAutocorrectionTypeNo;
    _txtTargetIP.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _txtTargetIP.clearButtonMode = UITextFieldViewModeAlways;
    _txtTargetIP.returnKeyType = UIReturnKeyNext;
    _txtTargetIP.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    _txtTargetIP.delegate = self;
    [self.view addSubview:_txtTargetIP];
    
    _labPassword = [[UILabel alloc] init];
    _labPassword.translatesAutoresizingMaskIntoConstraints = NO;
    _labPassword.text = @"密码:";
    _labPassword.textAlignment = NSTextAlignmentRight;
    _labPassword.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_labPassword];
    
    _txtPassword = [[UITextField alloc] init];
    _txtPassword.translatesAutoresizingMaskIntoConstraints = NO;
    _txtPassword.borderStyle = UITextBorderStyleRoundedRect;
    _txtPassword.placeholder = @"输入密码";
    _txtPassword.text = @"vision66";
    _txtPassword.autocorrectionType = UITextAutocorrectionTypeNo;
    _txtPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _txtPassword.clearButtonMode = UITextFieldViewModeAlways;
    _txtPassword.secureTextEntry = YES;
    _txtPassword.returnKeyType = UIReturnKeyDone;
    _txtPassword.keyboardType = UIKeyboardTypeDefault;
    _txtPassword.delegate = self;
    [self.view addSubview:_txtPassword];
    
    [self constraintsWithPortrait];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    NSLog(@"%s, frame=(%f,%f,%f,%f)", __FUNCTION__, self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%s, frame=(%f,%f,%f,%f)", __FUNCTION__, self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
}

- (void)constraintsWithPortrait
{
    if (self.view.constraints) [self.view removeConstraints:self.view.constraints];
    
    _labTargetIP.textAlignment = NSTextAlignmentLeft;
    _labPassword.textAlignment = NSTextAlignmentLeft;
    
    NSArray* constraints = [[NSArray alloc] initWithObjects:
                            
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:200.0],
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeCenterX  relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterX          multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant: 30.0],
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeBottom   relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:-50.0],
                            
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:200.0],
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeCenterX  relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterX          multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:_labTargetIP attribute:NSLayoutAttributeHeight           multiplier:0.9 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeBottom   relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:-10.0],
                            
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:200.0],
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeCenterX  relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterX          multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeTop      relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant: 10.0],
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant: 30.0],

                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:200.0],
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeCenterX  relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterX          multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeTop      relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant: 50.0],
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant: 30.0],

                            nil];
    
    [self.view addConstraints:constraints];
}

- (void)constraintsWithLandscape
{
    if (self.view.constraints) [self.view removeConstraints:self.view.constraints];
    
    _labTargetIP.textAlignment = NSTextAlignmentRight;
    _labPassword.textAlignment = NSTextAlignmentRight;
    
    NSArray* constraints = [[NSArray alloc] initWithObjects:
                            
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:100.0],
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeRight    relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeRight            multiplier:0.3 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant: 30.0],
                            [NSLayoutConstraint constraintWithItem:_labTargetIP attribute:NSLayoutAttributeBottom   relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:-10.0],
                            
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeLeft     relatedBy:NSLayoutRelationEqual toItem:_labTargetIP attribute:NSLayoutAttributeRight            multiplier:1.0 constant: 10.0],
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:200.0],
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:_labTargetIP attribute:NSLayoutAttributeHeight           multiplier:0.9 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_txtTargetIP attribute:NSLayoutAttributeCenterY  relatedBy:NSLayoutRelationEqual toItem:_labTargetIP attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:  0.0],
                            
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:100.0],
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeRight    relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeRight            multiplier:0.3 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeTop      relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant: 10.0],
                            [NSLayoutConstraint constraintWithItem:_labPassword attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant: 30.0],
                            
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeLeft     relatedBy:NSLayoutRelationEqual toItem:_labPassword attribute:NSLayoutAttributeRight            multiplier:1.0 constant: 10.0],
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant:200.0],
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual toItem:_labPassword attribute:NSLayoutAttributeHeight           multiplier:0.9 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_txtPassword attribute:NSLayoutAttributeCenterY  relatedBy:NSLayoutRelationEqual toItem:_labPassword attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:  0.0],
                            
                            nil];
    
    [self.view addConstraints:constraints];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        [UIView animateWithDuration:duration animations:^{
            [self constraintsWithPortrait];
            [self.view layoutIfNeeded];
        } completion:nil];
        
        return;
    }

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
    {
        [UIView animateWithDuration:duration animations:^{
            [self constraintsWithLandscape];
            [self.view layoutIfNeeded];
        } completion:nil];
        
        return;
    }
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    NSLog(@"%s, frame=(%f,%f,%f,%f)", __FUNCTION__, self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
}

- (void)gotoLive
{
    [[AppVariables variables] setTargetIP:_txtTargetIP.text];
    [[AppVariables variables] setPassword:_txtPassword.text];
    
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"oglFlip";
    transition.subtype = kCATransitionFromTop;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:@"animationxxxx"];
    
    UIVCLive* controller = [[UIVCLive alloc] init];
    [self.navigationController pushViewController:controller animated:NO];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

@end
